﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Compression;
using System.IO;

namespace Zeny.Common.Utils
{
    public class GzipUtils
    {
        private static int bufferSize = 128;

        public static byte[] zip(string s)
        {
            byte[] bs = TypeConverterUtils.stringToBytes(s);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Compress);
            sw.Write(bs, 0, bs.Length);
            sw.Close();

            bs = ms.ToArray();

            return bs;
        }

        public static string unzip(byte[] bs)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bs);
            System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Decompress);

            System.IO.MemoryStream result = new System.IO.MemoryStream();
            byte[] buffer = new byte[bufferSize];
            int readCount = 0;
            while ((readCount = sr.Read(buffer, 0, bufferSize)) != 0)
            {
                result.Write(buffer, 0, readCount);
            }

            sr.Close();

            bs = result.ToArray();
            
            String s = TypeConverterUtils.bytesToString(bs);
            return s;
        }
    }
}
